package com.ruoyi.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.flowable.domain.bo.FormVersionBo;
import com.ruoyi.flowable.domain.vo.FormVersionVo;
import com.ruoyi.flowable.domain.FormVersion;
import com.ruoyi.flowable.mapper.FormVersionMapper;
import com.ruoyi.flowable.service.IFormVersionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 表单版本Service业务层处理
 *
 * @author zxw
 * @date 2023-03-27
 */
@RequiredArgsConstructor
@Service
public class FormVersionServiceImpl implements IFormVersionService {

    private final FormVersionMapper baseMapper;

    /**
     * 查询表单版本
     */
    @Override
    public FormVersionVo queryById(Long versionId){
        return baseMapper.selectVoById(versionId);
    }

    /**
     * 查询表单版本列表
     */
    @Override
    public TableDataInfo<FormVersionVo> queryPageList(FormVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FormVersion> lqw = buildQueryWrapper(bo);
        Page<FormVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询表单版本列表
     */
    @Override
    public List<FormVersionVo> queryList(FormVersionBo bo) {
        LambdaQueryWrapper<FormVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FormVersion> buildQueryWrapper(FormVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FormVersion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFormId() != null, FormVersion::getFormId, bo.getFormId());
        return lqw;
    }

    /**
     * 新增表单版本
     */
    @Override
    public Boolean insertByBo(FormVersionBo bo) {
        FormVersion add = BeanUtil.toBean(bo, FormVersion.class);
        validEntityBeforeSave(add);

        Long max = baseMapper.selectMaxVersionByFormId(add.getFormId());
        if (max == null) max = 0L;
        add.setVersion(max + 1);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setVersionId(add.getVersionId());
        }
        return flag;
    }

    /**
     * 修改表单版本
     */
    @Override
    public Boolean updateByBo(FormVersionBo bo) {
        FormVersion update = BeanUtil.toBean(bo, FormVersion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FormVersion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除表单版本
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
